package com.zr.plant.controller;

import com.zr.meiju.StatusEnum;
import com.zr.meiju.StatusValueEnum;
import com.zr.meiju.StatusZhongWenEnum;
import com.zr.plant.pojo.*;
import com.zr.plant.service.PlantService;
import com.zr.util.AllRecords;
import com.zr.util.ResultBuildVo;
import com.zr.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by 86151 on 2019/6/14.
 */
@RestController
@RequestMapping("plant/")
@CrossOrigin
public class PlantController {
    @Autowired
    private PlantService plantService;

    /**
     * 新增接口
     * @param plantAddVo
     * @param bindingResult
     * @return
     */
    @PostMapping("add")
    public ResultVo addPlant(@RequestBody @Valid PlantAddVo plantAddVo, BindingResult bindingResult){
       if (bindingResult.hasErrors()){
           return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage(),"500");
       }
        return plantService.addPlant(plantAddVo);
    }

    /**
     * 修改接口
     * @param plantUpdateVo
     * @param bindingResult
     * @return
     */
    @PostMapping("update")
    public ResultVo updatePlant(@RequestBody @Valid PlantUpdateVo plantUpdateVo, BindingResult bindingResult){
       if (bindingResult.hasErrors()){
           return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage(),"500");
       }
        return plantService.updatePlant(plantUpdateVo);
    }

    /**
     * 修改状态接口
     * @param plantStatusVo
     * @param bindingResult
     * @return
     */
    @PostMapping("updateStatus")
    public ResultVo updateStatus(@RequestBody @Valid PlantStatusVo plantStatusVo, BindingResult bindingResult){
       if (bindingResult.hasErrors()){
           return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage(),"500");
       }
        return plantService.updateStatus(plantStatusVo);
    }



    /**
     * 法人下拉框
     */
    @GetMapping("queryDownByUserId")
    public ResultVo<List<Legalperson>> queryDownByUserId(@RequestParam("uid")Integer id){
        return plantService.queryDown(id);
    }


    /**
     * 工厂查看接口
     */
    @GetMapping("queryPlantById")
    public ResultVo<PlantClass> queryPlantById(@RequestParam("id")Integer id){
        return plantService.queryPlantById(id);
    }

    /**
     * 分页查询接口
     */
    @PostMapping("queryPage")
    public ResultVo<AllRecords> queryPage(@RequestBody PlantSelectVo plantSelectVo){

        return plantService.queryPage(plantSelectVo);
    }
    /**
     * 启用禁用下拉框
     */
    /*@PostMapping("metadata/getPullDownList")
    public ResultVo<List<XiaLaVo>> queryXiaLa(){
      *//*  List<XiaLaVo> xiaLaVoList = new ArrayList<>();
        XiaLaVo xiaLaVo = new XiaLaVo();
        xiaLaVo.setKey(StatusValueEnum.QIYONG.getStatus());
        xiaLaVo.setValue(StatusZhongWenEnum.QIYONG.getStatus());
        xiaLaVoList.add(xiaLaVo);
        XiaLaVo xiaLaVo2 = new XiaLaVo();
        xiaLaVo2.setKey(StatusValueEnum.JINYONG.getStatus());
        xiaLaVo2.setValue(StatusZhongWenEnum.JINYONG.getStatus());
        xiaLaVoList.add(xiaLaVo2);*//*
        List<XiaLaVo> xiaLaVoList = new ArrayList<>();
      for (int i= 0;i< StatusEnum.values().length;i++){
          XiaLaVo xiaLaVo = new XiaLaVo();
          xiaLaVo.setKey(i);
          xiaLaVo.setValue(StatusEnum.getName(i));
          xiaLaVoList.add(xiaLaVo);
      }
        return ResultBuildVo.success(xiaLaVoList);
    }*/
    /**
     * 修改，根据id进行数据的修改
     * 业务需求：修改时数据库中不能存在重复的名称
     * 如果用户修改的数据和原有数据一致的话不提示重复，只有和其他数据重复了才提示重复。
     */

    /**
     * 查看
     */
 /*   @GetMapping("see")
    public ResultVo queryPlanClassById(@RequestParam("id")Integer id){

        return plantService.queryPlanClassById(id);
    }*/
    /**
     * 分页查询
     */
  /*  @PostMapping("queryPage")
    public ResultVo<AllRecords> queryPage(@RequestBody PlantSelectVo plantSelectVo){

        return plantService.queryPage(plantSelectVo);
    }*/
}
